<table class="table table-striped goods_list_table   table-bordered">
    <thead>
        <tr>
            <th class="opth100" >选择</th>
        	<th class="opth100">采购单名称</th>
             <th class="opth100">供应商联系人</th>
             <th class="opth100">商品名称</th>
        	<th class="opth100">报价数量</th>
        	<th class="opth70">实际采用数量</th>
        	<th class="opth100">报价价格</th>
        	<th class="opth100">送货时间</th>
        	<th class="opth70">是否批次送货</th>
            <th class="opth70">创建时间</th>
        </tr>
    </thead>
    <tbody id="tbodylist">
        <volist name="_datalist['lists']" id="g">
            <tr>
                <td><input type="checkbox" field='"idcode":"{$g.code}","offerid":"{$g.offerid}","purchaseid":"{$g.purchaseid}","actual_number":"{$g.actual_number}","offer_price":"{$g.offer_price}","newprice":"{$g.offer_price}","sp_id":"{$g.sp_id}"' actualnumber="{$g['offer_number']}"  oldactualnumber="{$g['offer_number']}"  offerprice = "{$g['offer_price']}" newprice = "{$g['offer_price']}"/></td>
            	<td>{$g['purchaseame']}</td>
                <td>{$g['linkname']}</td>
                <td>{$g['goodsname']}</td>
                <td>{$g['offer_number']}</td>
            	 <td>0</td>
                <td>{$g['offer_price']}</td>
                <td>{$g['sendtime']}</td>
                <td>{$g['isbatch']==1?'是':'否'}</td>
                <td>{$g['createtime']}</td>
            </tr>
        </volist>
      
    </tbody>

</table>
<!-- <label ><input type="checkbox" name="whole"><span style="margin:0 20px 0 0;">全选</span></label>  -->
 <button class="btn btn-primary btn-large"  id = "submit" style="float:right;">审核</button>
<notempty name="_datalist['lists']">
  <div class="page_list_ops dataTables_paginate paging_bootstrap pagination">
    {$_datalist['page_html']}
 </div>	

</notempty>

<script type="text/javascript"> 
   var reg=/^\d+(\.\d{0,2})?$/;
$(function(){
$("#submit").attr("disabled", "disabled");
$('input[type="checkbox"]').bind("click",setgoodincookie);
 $("input[type='text']").bind("keyup",findtable);   
 $("#submit").bind('click',appsave);
 })


 
 //点击checkbox 
 function setgoodincookie()
 {
  debugger;
     switch_submit();
   var flg = $(this).is(":checked");
   var oldactualnumber = $(this).attr('oldactualnumber');
   var offer_price = $(this).attr('offerprice');
    if(flg == true){
        $(this).parent('td').parent('tr').children('td').eq(5).html('<input type="text" actualnumber="number" name = "number" value="'+oldactualnumber+'" style="width:80px;">');
        $(this).parent('td').parent('tr').children('td').eq(6).html('<input type="text" name = "offer_price" newprice="offer_price" value="'+offer_price+'" style="width:80px;">');
        $("input[type='text']").unbind("keyup",findtable);
        $("input[type='text']").bind("keyup",findtable);
     }else{
        $(this).parent('td').parent('tr').children('td').eq(5).html(oldactualnumber);
        $(this).parent('td').parent('tr').children('td').eq(6).html(offer_price);
        $(this).parent('td').parent('tr').children('td').eq(0).find('input').attr('actualnumber',oldactualnumber);
     }
 }  
 
//禁用或解开submit提交按钮
function switch_submit(){
   var flag = false;
    $("input[type='checkbox']").each(function() {
      var flg = $(this).is(":checked");
          if(flg){
            flag = true;
            $("#submit").removeAttr("disabled");
          }
    })
     if(flag ==true){
       $("#submit").removeAttr("disabled");
     }else{
        $("#submit").attr("disabled", "disabled");
     }
}


 //选中的checkbox 数量改成复选框以及对输入的值验证
function  findtable(){
  
    var _time=0;
   clearTimeout(_time);
   $_this = $(this);
   _time=setTimeout(function(){searchtable($_this);},300);
}
 //
function searchtable($_this)
{
  debugger;
           var lsjson=[];

         var name = $_this.attr('name');
          if(name == "number"){
            var actual_number = $_this.val();
            var offer_price = $_this.parent('td').parent('tr').children('td').eq(6).find('input').val();
          }else{
            var offer_price = $_this.val();
            var actual_number = $_this.parent('td').parent('tr').children('td').eq(5).find('input').val();
          }
          
          var offer_number = $_this.parent('td').prev().html();
          var  number = offer_number - actual_number ;
             if(!reg.test(actual_number)){
                alert('数量有误');
                return false;
              }

             if(!reg.test(offer_price)){
                alert('价格有误');
                return false;
              }
           if(actual_number <0){
               alert('数量有误');
                return false;
           }

          if(offer_price <0){
               alert('价格有误');
                return false;
           }
          if(number<0){
               alert('数量有误');
                return false;
           }
        $_this.parent('td').parent('tr').children('td').eq(0).find('input').attr("newprice",offer_price);
       $_this.parent('td').parent('tr').children('td').eq(0).find('input').attr("actualnumber",actual_number);
    
   } 

function  appsave()
 {
debugger;
       //获取选中的checkbox 并获取到里面的数量
    var data = [];
    $("#submit").attr("disabled", "disabled");  
    $("input[type='checkbox']").each(function() {

        var _json=$(this).attr("field");
        var _json="{"+_json+"}"
        var tmp =JSON.parse(_json);
          var flg = $(this).is(":checked");
          if(flg){
            var _actual_number = $(this).attr('actualnumber');
            var _offer_price = $(this).attr('newprice');
              tmp.actual_number=_actual_number; 
              tmp.offer_price=_offer_price; 
              data.push(tmp); 
          }

     }); 


if(data != ""){
    var _data = JSON.stringify(data);
}
debugger;
     $.ajax({
        "url": "{:U('save_approvalList')}",
        "data": {'list':_data},
         "dataType": "json",
         "type": "POST",
         success: function (data) {
             if(!data){
                 art.dialog({title:'温馨提示',content: '提交数据保存失败', ok: function () {this.close();return false;},okval:'确定'});
                 return;
             }

             art.dialog({title:'温馨提示',content: data.msg, ok: function () {this.close();return false;},okval:'确定'});
              setTimeout(function(){
                        var win=    art.dialog.opener;
                        win._reload_currentpage_datalist('load_tb_div');
                        art.dialog.close();
                          //_load_html_byurl("{:U('ApprovalList')}", "load_tb_div");
                          //location.href = "{:U('Approval/index')}";
                 },1000); 

         }, error: function (err) { 

             art.dialog({title:'温馨提示',content: err.responseText, ok: function () {this.close();return false;},okval:'确定'});
         },complete: function () {
            
         }
     });  
 }
 function closew(){
             setTimeout(function(){
               var win=    art.dialog.opener;
                        win._reload_currentpage_datalist('load_tb_div');
                        art.dialog.close();
             },300);
 }
</script>